const MD5 = require('crypto-js/md5')

module.exports = mongoose => {
  let model = {Schemas: {}}

  sub_student: {
    model.Schemas.student = new mongoose.Schema({
      id: mongoose.Schema.ObjectId,
      no: Number,
      name: String
    })
  }

  doc:
  {
    let schema = model.Schemas.self = new mongoose.Schema({
      username: { type: String, unique: true },
      password: { type: String },
      nick: { type: String },
      mobile: { type: String, unique: true, sparse: true}, // Todo, use class
      email: { type: String, unique: true, sparse: true},
      user_type: { type: String },
      user_comment: {
        student_id: { type: mongoose.Schema.ObjectId, requried: false },
        teacher_id: { type: mongoose.Schema.ObjectId, requried: false },
        student: { type: model.Schemas.student, requried: false }
      }
    }, { collection: 'user' })

    schema.virtual('hash_password').get(function () {
      let unitArr = new MD5(this.password)
      return unitArr + ''
    })

    schema.pre('save', function (next) {
      this.password = this.hash_password
      next()
    })

    model.doc = mongoose.model('User', schema)
    model.item = model.doc
  }

  return model
}
